package com.ly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.entity.Activity;
import com.ly.pojo.dto.ActivityDTO;
import com.ly.pojo.vo.ActivityVO;
import com.ly.service.ActivityService;
import com.ly.utils.ExcludeToken;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 黑渊白花
 * @ClassName ActivityController
 * @date : 2025/2/10 21:48
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/activity")
@Slf4j
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @GetMapping("/list")
    @ExcludeToken
    public Result list(){
        List<Activity> list = activityService.list();
        return Result.success(list);
    }

    //活动总数
    @GetMapping("/getActivityNum")
    @ExcludeToken
    public Result getActivityNum(){
        long count = activityService.count();
        return Result.success(count);
    }

    //某一社团下有多少个活动
    @GetMapping("/getActNumByTeamId/{teamId}")
    @ExcludeToken
    public Result getActNumByTeamId(@PathVariable Integer teamId){
        long count = activityService.getActNumByTeamId(teamId);
        return Result.success(count);
    }

    //获取参加人数最多的5个活动
    @GetMapping("/getActEnterNum")
    @ExcludeToken
    public Result getActEnterNum(){
        List<Activity> tActivityEnterNum = activityService.getTActivityEnterNum();
        return Result.success(tActivityEnterNum);
    }

    //某一社团下最新发布的5个活动
    @GetMapping("/getFiveByTeamId/{teamId}")
    @ExcludeToken
    public Result getFiveByTeamId(@PathVariable Integer teamId){
        List<Activity> tActivityEnterNum = activityService.getFiveByTeamId(teamId);
        return Result.success(tActivityEnterNum);
    }


    //某一社团下每个活动都有多少个参加的(最新发布的5个活动)
    @GetMapping("/getEnterNumByTeamId/{teamId}")
    public Result getEnterNumByTeamId(@PathVariable Integer teamId){
        List<Activity> tActivityEnterNum = activityService.getEnterNumByTeamId(teamId);
        return Result.success(tActivityEnterNum);
    }

    //审核通过的活动（status=2）
    @GetMapping("/Allpage")
    public Object Allpage( ActivityDTO activityDTO){
        log.info("{}",activityDTO);
        IPage<ActivityVO> page=null;
        page= activityService.getAllPage(activityDTO);
        return Result.success(page);
    }

    //社长的活动全部
    @GetMapping("/page")
    public Object page( ActivityDTO activityDTO){
        log.info("{}",activityDTO);
        IPage<ActivityVO> page=null;
        page= activityService.getPage(activityDTO);
        return Result.success(page);
    }

    //所有活动
    @GetMapping("/pageAudit")
    public Object pageAudit( ActivityDTO activityDTO){
        log.info("{}",activityDTO);
        IPage<ActivityVO> auditPage = activityService.getAuditPage(activityDTO);
        return Result.success(auditPage);
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Activity activity){
        activityService.updateById(activity);
        return Result.success();
    }

    //添加
    @PostMapping("/save")
    public Result save(@RequestBody Activity activity){
        activityService.save(activity);
        return Result.success();
    }

    //删除的时候同时把下面的记录同时删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        boolean b = activityService.deleteAll(id);
        if(b==false){
            return Result.warn("删除失败");
        }
        return Result.success("删除成功");
    }

    //通过活动id得到活动信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        ActivityVO actById = activityService.getActById(id);
        return Result.success(actById);
    }

    //通过社团类型名搜索活动
    @GetMapping("/searchActByType/{typeName}")
    public Result searchActByType(@PathVariable String typeName){
        List<ActivityVO> activityVOS = activityService.searchActByType(typeName);
        return Result.success(activityVOS);
    }

    @GetMapping("/getActsByStuId/{stuId}")
    public Result getActsByStuId(@PathVariable Integer stuId){
        List<ActivityVO> actsByStuId = activityService.getActsByStuId(stuId);
        return Result.success(actsByStuId);
    }


}
